Repository navigation
Add Brain Network Transformer for connectome classification - #1766
Conversation
Port BrainNetworkTransformer (Kan et al., NeurIPS 2022) from Wayfear/BrainNetworkTransformer (MIT License) without the omegaconf dependency. The model reads a square connectivity matrix, runs a transformer over the nodes and pools them with the orthonormal clustering readout. The number of nodes comes from the dataset, the classifier is sized by the label processor, and the attention weights and cluster assignments stay available. The encoder layer is written out instead of subclassing nn.TransformerEncoderLayer: the official override does not accept the is_causal argument of PyTorch 2, and its positional constructor call drops the layer biases on PyTorch 2.1 and later. bnt_parity_check.py loads the official weights into the port and finds identical logits and cluster assignments. abide_autism_bnt.py trains BNT, the GCN baseline and logistic regression on the same patient splits over several seeds.
Test CoverageTotal: 88.62% (60323 / 68071 lines)
|
|
/review |
| The defaults are the authors' configuration: two layers, the first | ||
| keeping the nodes and the second pooling them into 100 clusters, 4 heads, | ||
| a feed-forward size of 1024, no positional encoding. The number of nodes | ||
| is read from the dataset, so any atlas works. With |
There was a problem hiding this comment.
Fixed in 816b5bd: the class docstring and the docs page now say that the default 100 orthogonal clusters need at least 100 features per node (cc200 and larger atlases), and that smaller graphs need a smaller layer_sizes. The model also raises a ValueError that names layer_sizes in that case, with a test.
| mixup of the inputs and labels during training. Known differences with | ||
| the official run: the learning rate stays constant (the official run | ||
| decays it from 1e-4 to 1e-5 with a cosine schedule), PyHealth's | ||
| ``Trainer`` exempts biases and layer norms from weight decay (the |
There was a problem hiding this comment.
Right, thanks: the Trainer exempts names containing bias, LayerNorm.bias or LayerNorm.weight, so norm1.weight and norm2.weight are decayed. Fixed in 816b5bd: the docstring now says only the biases are exempt, and that the layer norm weights are decayed in both runs.
There was a problem hiding this comment.
Previous review
AI Review
suggestion: need change
decision: need change
Thank you for this contribution. It adds the Brain Network Transformer (Kan et al., NeurIPS 2022) as a PyHealth model. It comes with a docs page, 21 unit tests, a parity check against the official code, and a same-split comparison with the GCN and logistic regression baselines. The model is self-contained and well tested. Coverage is 98% for brain_network_transformer.py and 88.6% for the repository, both above the bar. The code and tests look sound. Two documentation statements don't match the code, so I'm asking you to fix them before merge.
To get this merged:
- Atlas claim. The class docstring and the docs page say the model reads the node count from the data, so "any atlas works". With the default settings, the second layer builds 100 orthogonal centres in a node-feature space whose size equals the node count.
ClusterAssignmentraisesValueErrorwhencluster_number > embedding_dimension. So any graph under 100 nodes fails unless the user passeslayer_sizeswith a smaller cluster count. Please either reword the claim, for example "any atlas with at least 100 nodes, or setlayer_sizesfor smaller ones", or make the default cluster count adapt. - Weight-decay claim. The
abide_autism_bnt.pydocstring says PyHealth'sTrainerexempts biases and layer norms from weight decay. The first reviewer reports that theTrainermatches only parameter names containingLayerNorm.. This model'snorm1andnorm2layers would then still be decayed. Please check this against theTrainerand correct the sentence. The "known differences" list matters because you compare against the paper's numbers.
Optional suggestions:
- Add a small test for
MixupBrainNetworkTransformer. For example, check that eval mode leaves the batch unchanged and that training mode mixes both inputs and labels. - The parity check needs an external clone and
omegaconf. You could mention in the docs that it isn't run in CI.
Reviewed commit 6ea9136
The default 100 orthogonal clusters need at least 100 features per node, so the docstring and docs page no longer say that any atlas works, and the model now raises a ValueError naming layer_sizes when a pooling layer asks for more clusters than that. The example states that the Trainer exempts only the biases from weight decay, and the docs page says the parity check is not run in CI.
| The defaults are the authors' configuration: two layers, the first | ||
| keeping the nodes and the second pooling them into 100 clusters, 4 heads, | ||
| a feed-forward size of 1024, no positional encoding. The number of nodes | ||
| is read from the dataset, so any atlas works. With |
There was a problem hiding this comment.
Fixed in 816b5bd: the class docstring and the docs page now say that the default 100 orthogonal clusters need at least 100 features per node (cc200 and larger atlases), and that smaller graphs need a smaller layer_sizes. The model also raises a ValueError that names layer_sizes in that case, with a test.
| mixup of the inputs and labels during training. Known differences with | ||
| the official run: the learning rate stays constant (the official run | ||
| decays it from 1e-4 to 1e-5 with a cosine schedule), PyHealth's | ||
| ``Trainer`` exempts biases and layer norms from weight decay (the |
There was a problem hiding this comment.
Right, thanks: the Trainer exempts names containing bias, LayerNorm.bias or LayerNorm.weight, so norm1.weight and norm2.weight are decayed. Fixed in 816b5bd: the docstring now says only the biases are exempt, and that the layer norm weights are decayed in both runs.
|
On the optional points: the docs page now says the parity check needs a clone of the official code and |
|
/review |
There was a problem hiding this comment.
AI Review
suggestion: approve
decision: approve
Thank you for contributing the Brain Network Transformer. It adds a faithful, well-documented port of the NeurIPS 2022 model for ABIDE connectome classification. It also adds a parity check against the official code and a comparison script that runs BNT, the GCN example and logistic regression on the same splits.
It looks ready to merge.
- Scope: The change is self-contained. The only edits to existing files are the export line in
models/__init__.pyand the docs entry. No base class, Trainer or shared utility is touched. - Sharing: The example imports
BrainGCNfrom the GCN example and usesLogisticRegressionas a baseline. The description names both as comparison baselines, which explains the links. - Coverage: The new model file has 98.2% coverage, above the 80% bar. The whole repository is at 88.62%, above the 60% bar.
__init__.pyis at 100%. - Docs: The docs page matches the code. The paper and the official repository are cited, along with their licences.
- Limits: The description states the gap to the paper's results, and that BNT's lead over logistic regression is not established. That is an honest limitation and not a blocker.
Optional suggestions:
- The description says 21 new tests, but the test file appears to contain 22. You could correct the count.
- The parity-check and training scripts have no CI test, which is understandable given their external dependencies. A short smoke test of the mixup wrapper would be a nice extra.
Reviewed commit 816b5bd
Contributor: Axel Coiffard-Donaty (@AxelNoun)
Contribution Type: New model
Description:
Adds Brain Network Transformer (Kan et al., NeurIPS 2022), ported from Wayfear/BrainNetworkTransformer (MIT) without
omegaconf. It reads the(nodes, nodes)connectivity ofABIDEAutismClassificationand pools the nodes with the orthonormal clustering readout; attention weights and cluster assignments stay available.The encoder layer is written out instead of subclassing
nn.TransformerEncoderLayer: on PyTorch 2 the official override rejectsis_causal, and its positional constructor call builds the layer without biases from PyTorch 2.1 on.Files to Review:
pyhealth/models/brain_network_transformer.pytests/core/test_brain_network_transformer.pyexamples/fmri/bnt_parity_check.py: comparison with the official code.examples/fmri/abide_autism_bnt.py: BNT, the GCN example and logistic regression on the same patient splits.Testing:
tools/check_pr_rules.py: passed.Results (%, ABIDE, 1,035 scans, 70/10/20 patient split, 5 seeds, test at the best validation AUROC epoch; baselines keep their examples' settings):
Limits: the gap to the paper is not explained yet. Known differences: constant learning rate (no cosine decay), split not stratified by site, PyHealth's cohort (1,035 scans, the paper 1,009 subjects). Over 5 seeds, the lead of BNT over logistic regression (2.3 points on average, from -0.7 to 5.0) is not established.